home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8751 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem Negating an Unsigned Char
  5. Date: 6 Mar 1996 00:34:52 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hjincINNpp0@keats.ugrad.cs.ubc.ca>
  8. References: <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl> <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> <Dntr8t.L41@cwi.nl>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Dntr8t.L41@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
  12.  >In article <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13.  > > In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
  14.  > >  > > To make it portable, you must manually mask for the lower eight bits:
  15.  > >  > > 
  16.  > >  > >     if (a == (~b & 0xff))
  17.  > >  >
  18.  > >  >And how would this work on machines with other than eight bit chars?
  19.  > > 
  20.  > > Just fine, thank you. The C standard guarantees char to be at least eight
  21.  > > bits wide. The original poster wants eight bit arithmetic; i.e. that the
  22.  > > complement of 0x11 be equal to 0xEE. This is what he is testing for, and
  23.  > > the above is how you get it.
  24.  >
  25.  >Well, that is your interpretation of what the original poster wants.  See
  26.  >the subject line.  As I read it he just wanted to know why
  27.  >    a == ~b
  28.  >does not work and why
  29.  >    a == (unsigned char)(~b)
  30.  >does work.
  31.  >...
  32.  > > Casting to an unsigned quantity is implementation defined. Are you suggesting
  33.  > > that the invocation of implementation defined behavior is more portable than a
  34.  > > well-defined bit masking operation?
  35.  >
  36.  >I know that negating unsigned chars with CHAR_BIT bits is difficult to do
  37.  >portably (see for instance Peter Seebach's posts).  And, yes, I know that
  38.  >casting is likely to fail on a 1's complement machine.  But of the 1's
  39.  
  40. Casting to a narrower unsigned type from a signed type does not fail. It is
  41. well-defined as doing a congruential operation (it finds the smallest positive
  42. residue of the signed number modulo the power of two that represents the
  43. unsigned quantity).
  44.  
  45. What does fail is assuming that if you negate the bits of a signed integral
  46. type, you get a particular integer value, and hence a particular congruence
  47. class. That is what is the culprit in this problem, not the cast itself.
  48. -- 
  49.  
  50.